//dom工具类
const elementUtil = {
    formatRichText: function (html) {
        //控制小程序中图片大小
        let newContent = ""
        if (typeof html == "string") {
            //检测值为字符串才能进行replace操作
            newContent = html.replace(/<img[^>]*>/gi, function (match) {
                match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "")
                match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "")
                match = match.replace(/height="[^"]+"/gi, "").replace(/height='[^']+'/gi, "")
                return match
            })
            newContent = newContent.replace(/style="[^"]+"/gi, function (match) {
                match = match.replace(/width:[^;]+;/gi, "max-width:100%;").replace(/width:[^;]+;/gi, "max-width:100%;")
                return match
            })
            newContent = newContent.replace(/<br[^>]*\/>/gi, "")
            newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
            return newContent
        }
    },
    getQueryVariable: function (variable) {
        var query = window.location.href.substring(1)
        var vars = query.split("&")
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=")
            if (pair[0] == variable) {
                return pair[1]
            }
        }
        return false
    },
    getSwiperHeight: function (obj, num, domClass, callBack) {
        let query = uni.createSelectorQuery().in(obj)
        query.selectAll(domClass).boundingClientRect()
        query.exec((res) => {
            let height = num + res[0][obj.tabIndex].height
            callBack(height)
        })
    },
    getElementHeight(obj, element, callBack) {
        //获取高度
        uni.getSystemInfo({
            //调用uni-app接口获取屏幕高度
            success(res) {
                //成功回调函数
                let ph = res.windowHeight
                uni.createSelectorQuery()
                    .in(obj)
                    .select(element)
                    .boundingClientRect()
                    .exec((ret) => {
                        if (ret) {
                            callBack(ph - ret[0].top)
                        }
                    })
            },
        })
    },
    getElementHeightForTwo(obj, element1, element2, callBack) {
        //获取高度
        uni.getSystemInfo({
            //调用uni-app接口获取屏幕高度
            success(res) {
                //成功回调函数
                let ph = res.windowHeight
                uni.createSelectorQuery()
                    .in(obj)
                    .select(element1)
                    .boundingClientRect()
                    .exec((ret) => {
                        if (ret) {
                            let ele1Height = ph - ret[0].top
                            uni.createSelectorQuery()
                                .in(obj)
                                .select(element2)
                                .boundingClientRect()
                                .exec((ret2) => {
                                    if (ret2) {
                                        let ele2Height = ph - ret2[0].top
                                        callBack(ele1Height - ele2Height)
                                    }
                                })
                        }
                    })
            },
        })
    },
    getElementTop(obj, element, callBack, isAddStatusBar = false) {
        //获取top值
        uni.getSystemInfo({
            //调用uni-app接口获取屏幕高度
            success(res) {
                //成功回调函数
                uni.createSelectorQuery()
                    .in(obj)
                    .select(element)
                    .boundingClientRect()
                    .exec((ret) => {
                        if (ret) {
                            let statusBar = isAddStatusBar ? 0 : res.windowTop
                            let top = ret[0].top + statusBar + "px"
                            callBack(top)
                        }
                    })
            },
        })
    },
    getDocumentScrollTop(obj, callBack) {
        //获取Document当前滚动高度
        uni.getSystemInfo({
            //调用uni-app接口获取屏幕高度
            success(res) {
                //成功回调函数
                uni.createSelectorQuery()
                    .in(obj)
                    .selectViewport()
                    .scrollOffset()
                    .exec((ret) => {
                        if (ret) {
                            callBack(ret[0].scrollTop)
                        }
                    })
            },
        })
    },

    /**
     * body禁止滚动（弹窗时禁止底部滚动）
     *
     */
    setNoScroll() {
        //#ifdef H5
        document.body.style.overflow = "hidden"
        //#endif
        //#ifdef MP-WEIXIN
        // wx.createSelectorQuery().select('page').style.overflow = "hidden"
        //#endif
    },

    /**
     * body禁止滚动清除
     */
    setScroll() {
        //#ifdef H5
        document.body.style.overflow = "auto"
        //#endif
        //#ifdef MP-WEIXIN
        // wx.createSelectorQuery().select('page').style.overflow = "auto"
        //#endif
    },
}

module.exports = {
    formatRichText: elementUtil.formatRichText,
    getQueryVariable: elementUtil.getQueryVariable,
    getSwiperHeight: elementUtil.getSwiperHeight,
    getElementHeight: elementUtil.getElementHeight,
    getElementHeightForTwo: elementUtil.getElementHeightForTwo,
    getElementTop: elementUtil.getElementTop,
    getDocumentScrollTop: elementUtil.getDocumentScrollTop,
    setNoScroll: elementUtil.setNoScroll,
    setScroll: elementUtil.setScroll,
}
